package com.whl.sm.service.impl.notice;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whl.sm.common.result.Result;
import com.whl.sm.mapper.notice.AttachmentMapper;
import com.whl.sm.model.notice.Attachment;
import com.whl.sm.service.notice.AttachmentService;
import com.whl.sm.service.system.OrgUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuhaolun
 * @since 2021-11-24
 */
@Service
@Slf4j
public class AttachmentServiceImpl extends ServiceImpl<AttachmentMapper, Attachment> implements AttachmentService {

    @Autowired
    private OrgUserService orgUserService;

    @Override
    public Result update(Attachment attachment) {
        try {
            attachment.setUploadUserId(orgUserService.getCurrentUserId());
            if (Objects.isNull(attachment.getId())){
                getBaseMapper().insert(attachment);
            }else {
                getBaseMapper().updateById(attachment);
            }
        }catch (Exception e){
            log.error("附件操作异常:"+e.getMessage());
            return Result.fail();
        }
        return Result.ok();
    }

    @Override
    public List<Attachment> getByDataId(String dataId) {
        List<Attachment> resultList = new ArrayList<>();
        try{
            resultList = getBaseMapper().selectList(new QueryWrapper<Attachment>()
                    .eq("data_id",dataId)
            );
            return resultList;
        }catch (Exception e){
            log.error("获取附件列表异常:"+e.getMessage());
            return resultList;
        }
    }
}
